<template>
  <div class="synchronous">
    <div class="content" v-show="showlist">
      <div class="plantime">
        <div class="left">
          <el-form label-width="auto">
            <el-row>
              <el-col>
                <el-form-item label=" 停机计划时间段：">
                  <el-date-picker
                    style="width: 330px"
                    v-model="timeDate"
                    size="mini"
                    type="datetimerange"
                    start-placeholder="开始时间"
                    end-placeholder="结束时间"
                    :default-time="['12:00:00']"
                    value-format="yyyy-MM-dd HH:mm:ss"
                    @change="changeValue"
                  >
                  </el-date-picker>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </div>
        <div class="right">
          <el-button
            v-waves
            class="filter-item"
            type="primary"
            icon="el-icon-search"
            @click="handleFilter"
          >
            {{ $t("table.search") }}
          </el-button>
        </div>
      </div>

      <!-- ---- -->
      <div class="contentT">
        <div class="tips">近1个月停机维护同步信息</div>
        <el-table
           height="750"
          :key="tableKey"
          v-loading="listLoading"
          :data="list"
          :header-cell-style="{
            background: '#EAF1FF',
            'text-align': 'center',
            color: '#000',
          }"
          :cell-style="{ 'text-align': 'center' }"
          fit
          highlight-current-row
          @sort-change="sortChange"
          style="width: 100%"
          :border="false"
        >
          <el-table-column
            label="序号"
            prop="id"
            sortable="custom"
            align="center"
            width="80"
          >
            <template slot-scope="{ row }">
              <span>{{ row.id }}</span>
            </template>
          </el-table-column>
          <el-table-column  label="报送时间" width="150px" align="center">
            <template slot-scope="{ row }">
              <span>{{
                row.timestamp | parseTime("{y}-{m}-{d} {h}:{i}")
              }}</span>
            </template>
          </el-table-column>
          <el-table-column label="停机主体机构标识" width="146px">
            <template slot-scope="{ row }">
              <span>{{ row.author }}</span>
            </template>
          </el-table-column>
          <el-table-column label="停机时间段" min-width="347px" align="center">
            <template slot-scope="{ row }">
              <span class="link-type" @click="handleUpdate(row)">{{
                row.title
              }}</span>
              <el-tag>{{ row.type | typeFilter }}</el-tag>
            </template>
          </el-table-column>

          <el-table-column label="运营信息类型" width="166px" align="center">
            <template slot-scope="{ row }">
              <span style="color: red">{{ row.reviewer }}</span>
            </template>
          </el-table-column>
          <el-table-column label="影响业务程度" width="127px">
            <template slot-scope="{ row }">
              <svg-icon
                v-for="n in +row.importance"
                :key="n"
                icon-class="star"
                class="meta-item__icon"
              />
            </template>
          </el-table-column>
          <el-table-column label="中断时长" align="center" width="95">
            <template slot-scope="{ row }">
              <span
                v-if="row.pageviews"
                class="link-type"
                @click="handleFetchPv(row.pageviews)"
                >{{ row.pageviews }}</span
              >
              <span v-else>0</span>
            </template>
          </el-table-column>
          <el-table-column
            label="业务状态"
            class-name="status-col"
            width="112px"
          >
            <template slot-scope="{ row }">
              <el-tag :type="row.status | statusFilter">
                {{ row.status }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column
            label="上报状态"
            class-name="status-col"
            width="112px"
          >
            <template slot-scope="{ row }">
              <el-tag :type="row.status | statusFilter">
                {{ row.status }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column label="描述信息" width="166px">
            <!-- <template slot-scope="{ row }">
            <el-tag :type="row.status | statusFilter">
              {{ row.status }}
            </el-tag>
          </template> -->
          </el-table-column>
          <el-table-column
            label="详情"
            align="center"
            width="106px"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="{ row, $index }">
              <el-button type="primary" size="mini" @click="handleUpdate(row)">
                {{ $t("table.edit") }}
              </el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="listQuery.page"
          :limit.sync="listQuery.limit"
          @pagination="getList"
        />
      </div>
    </div>
    <transition name="fade-transform" mode="out-in">
      <MessageSynDetailCom v-show="showdetail" />
    </transition>
  </div>
</template>

<script>
import MessageSynDetailCom from "./MessageSynDetail.vue";
import { fetchList, fetchPv } from "@/api/article";
import waves from "@/directive/waves"; // waves directive
import { parseTime } from "@/utils";
import Pagination from "@/components/Pagination"; // secondary package based on el-pagination

const calendarTypeOptions = [
  { key: "CN", display_name: "China" },
  { key: "US", display_name: "USA" },
  { key: "JP", display_name: "Japan" },
  { key: "EU", display_name: "Eurozone" },
];
// arr to obj, such as { CN : "China", US : "USA" }
const calendarTypeKeyValue = calendarTypeOptions.reduce((acc, cur) => {
  acc[cur.key] = cur.display_name;

  return acc;
}, {});

export default {
  name: "ComplexTable",
  components: { Pagination, MessageSynDetailCom },
  directives: { waves },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: "success",
        draft: "info",
        deleted: "danger",
      };
      return statusMap[status];
    },
    typeFilter(type) {
      return calendarTypeKeyValue[type];
    },
  },
  data() {
    return {
      showlist: true,
      showdetail: false,

      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      timeDate: [], //绑定model
      data: {
        kssj: "", //开始时间
        jssj: "", //结束时间
      },
      listQuery: {
        page: 1,
        limit: 20,

        importance: undefined,
        title: undefined,
        type: undefined,
        sort: "+id",
      },

      temp: {
        id: undefined,
        importance: 1,
        remark: "",
        timestamp: new Date(),
        title: "",
        type: "",
        status: "published",
      },

      // dialogFormVisible: false,
      // dialogStatus: "",
      // textMap: {
      //   update: "Edit",
      //   create: "Create",
      // },
      // dialogPvVisible: false,
      // pvData: [],
      // rules: {
      //   type: [
      //     { required: true, message: "type is required", trigger: "change" },
      //   ],
      //   timestamp: [
      //     {
      //       type: "date",
      //       required: true,
      //       message: "timestamp is required",
      //       trigger: "change",
      //     },
      //   ],
      //   title: [
      //     { required: true, message: "title is required", trigger: "blur" },
      //   ],
      // },
      downloadLoading: false,
    };
  },
  created() {
    this.getList();
    // console.log(this.temp);
  },
  mounted() {
    // this.$eventBus.$on("zipao", () => {
    //   (this.showlist = true), (this.showdetail = false);
    // });
  },
  methods: {
    getList() {
      this.listLoading = true;
      fetchList(this.listQuery).then((response) => {
        console.log(response);
        this.list = response.data.items;
        this.total = response.data.total;

        // Just to simulate the time of the request
        setTimeout(() => {
          this.listLoading = false;
        }, 1.5 * 1000);
      });
    },

    // 搜索
    handleFilter() {
      this.data.kssj = this.timeDate == null ? "" : this.timeDate[0];
      this.data.jssj = this.timeDate == null ? "" : this.timeDate[1];
      console.log(this.data);

      this.listQuery.page = 1;
      this.getList();
    },
    //组件的 chang 事件
    changeValue(val) {
      if (val) {
        this.data.kssj = val[0];
        this.data.jssj = val[1];
      } else {
        this.data.kssj = "";
        this.data.jssj = "";
      }
    },

    // handleModifyStatus(row, status) {
    //   this.$message({
    //     message: "操作成功",
    //     type: "success",
    //   });
    //   row.status = status;
    // },
    sortChange(data) {
      const { prop, order } = data;
      if (prop === "id") {
        this.sortByID(order);
      }
    },
    sortByID(order) {
      if (order === "ascending") {
        this.listQuery.sort = "+id";
      } else {
        this.listQuery.sort = "-id";
      }
      this.handleFilter();
    },
    resetTemp() {
      this.temp = {
        id: undefined,
        importance: 1,
        remark: "",
        timestamp: new Date(),
        title: "",
        status: "published",
        type: "",
      };
    },
    // handleCreate() {
    //   this.resetTemp()
    //   this.dialogStatus = 'create'
    //   this.dialogFormVisible = true
    //   this.$nextTick(() => {
    //     this.$refs['dataForm'].clearValidate()
    //   })
    // },
    // createData() {
    //   this.$refs["dataForm"].validate((valid) => {
    //     if (valid) {
    //       this.temp.id = parseInt(Math.random() * 100) + 1024; // mock a id
    //       this.temp.author = "vue-element-admin";
    //       createArticle(this.temp).then(() => {
    //         this.list.unshift(this.temp);
    //         this.dialogFormVisible = false;
    //         this.$notify({
    //           title: "成功",
    //           message: "创建成功",
    //           type: "success",
    //           duration: 2000,
    //         });
    //       });
    //     }
    //   });
    // },
    handleUpdate(row) {
      // this.showlist = false;
      // this.showdetail = true;
      this.$router.push({
        path: `/Technology/DownTime/MessageSynchronousDetail/${row.uuid}`,
        query: {},
      });
    },
    // updateData() {
    //   this.$refs["dataForm"].validate((valid) => {
    //     if (valid) {
    //       const tempData = Object.assign({}, this.temp);
    //       tempData.timestamp = +new Date(tempData.timestamp); // change Thu Nov 30 2017 16:41:05 GMT+0800 (CST) to 1512031311464
    //       updateArticle(tempData).then(() => {
    //         const index = this.list.findIndex((v) => v.id === this.temp.id);
    //         this.list.splice(index, 1, this.temp);
    //         this.dialogFormVisible = false;
    //         this.$notify({
    //           title: "成功",
    //           message: "更新成功",
    //           type: "success",
    //           duration: 2000,
    //         });
    //       });
    //     }
    //   });
    // },
    // handleDelete(row, index) {
    //   this.$notify({
    //     title: '成功',
    //     message: '删除成功',
    //     type: 'success',
    //     duration: 2000
    //   })
    //   this.list.splice(index, 1)
    // },
    handleFetchPv(pv) {
      fetchPv(pv).then((response) => {
        this.pvData = response.data.pvData;
        this.dialogPvVisible = true;
      });
    },
    // handleDownload() {
    //   this.downloadLoading = true
    //   import('@/vendor/Export2Excel').then(excel => {
    //     const tHeader = ['timestamp', 'title', 'type', 'importance', 'status']
    //     const filterVal = ['timestamp', 'title', 'type', 'importance', 'status']
    //     const data = this.formatJson(filterVal)
    //     excel.export_json_to_excel({
    //       header: tHeader,
    //       data,
    //       filename: 'table-list'
    //     })
    //     this.downloadLoading = false
    //   })
    // },
    // formatJson(filterVal) {
    //   return this.list.map((v) =>
    //     filterVal.map((j) => {
    //       if (j === "timestamp") {
    //         return parseTime(v[j]);
    //       } else {
    //         return v[j];
    //       }
    //     })
    //   );
    // },
    getSortClass: function (key) {
      const sort = this.listQuery.sort;
      return sort === `+${key}` ? "ascending" : "descending";
    },
  
  },
};
</script>

<style lang='scss' scoped>
@import "@/styles/Fadeinout.scss";
.synchronous {
  padding: 20px;
  .plantime {
    background-color: #fff;
    width: 100%;
    height: 64px;
    border-radius: 8px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    text-align: center;
    padding: 20px;
    font-size: 14px;
    box-shadow: 0px 2px 4px 0px rgba(0, 0, 0, 0.1);
    margin-bottom: 20px;
    .left {
      margin-top: 22px;
    }
  }
  .contentT {
    box-shadow: 0px 2px 4px 0px rgba(0, 0, 0, 0.1);
    border-radius: 8px;
    background-color: #fff;
    height: 100%;
    width: 100%;
    padding: 20px;

    .tips {
      width: 100%;
      height: 22px;
      font-size: 16px;
      // margin-left: 20px;
      font-weight: 500;
      color: #5084fb;
      line-height: 22px;
      margin-bottom: 20px;
    }
  }
}
</style>